/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-07-09
 * Time: 17:48
 */
//演示前缀和的思路,假如要求[L,R]的累加和
    //就是准备一个前缀和数组，每个元素的值就是原数组从0到i的前缀和，
    //比如数组[1,2,3,4,5]，前缀和数组就是[1,3,6,10,15]，
public class PrefixCombination {
    public static int[] prefixSum;
    private static void getPrefixSum(int[] arr) {
        prefixSum = new int[arr.length];
        prefixSum[0] = arr[0];
        for (int i = 1; i < arr.length; i++) {
            prefixSum[i] = prefixSum[i - 1] + arr[i];
        }
    }
    //求[L,R]的累加和
    public static int getSum(int L,int R){
        return L==0 ? prefixSum[R] : prefixSum[R]-prefixSum[L-1];
    }
    public static void main (String[] args) {
        int[] arr={1,2,3,4,5};
        getPrefixSum(arr);
        System.out.println( getSum( 1 , 3 ) );
    }
}
